# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Arch Haskell Team <arch-haskell@haskell.org>

_hkgname=ShellCheck
pkgname=shellcheck
pkgver=0.9.0
pkgrel=36
pkgdesc="Shell script analysis tool"
url="https://www.shellcheck.net"
license=("GPL")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-aeson' 'haskell-diff' 'haskell-fgl' 'haskell-quickcheck'
         'haskell-regex-tdfa')
makedepends=('ghc' 'pandoc' 'uusi')
source=("$_hkgname-$pkgver.tar.gz::https://github.com/koalaman/shellcheck/archive/v$pkgver.tar.gz")
sha512sums=('5931842b3b496c2a73db2576e89eede042a30e14dd98dec8fa36d58243acd1143a35645dd973f8c24f189ce760c746efd3d527209c2f9f1d044af372f2b9afea')

prepare() {
    cd $pkgname-$pkgver
    gen-setup
}

build() {
    cd $pkgname-$pkgver

    runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
        --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
        --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
        --ghc-option='-pie'
    runhaskell Setup build $MAKEFLAGS
    runhaskell Setup register --gen-script
    runhaskell Setup unregister --gen-script
    sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
    sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh

    ./manpage
}

check() {
    cd $pkgname-$pkgver
    # https://github.com/koalaman/shellcheck/issues/2677
    runhaskell Setup test --show-details=direct || echo "Tests failed"
}

package() {
    cd $pkgname-$pkgver

    install -D -m644 $pkgname.1    "${pkgdir}/usr/share/man/man1/${pkgname}.1"
    install -D -m744 register.sh   "${pkgdir}/usr/share/haskell/register/${pkgname}.sh"
    install -D -m744 unregister.sh "${pkgdir}/usr/share/haskell/unregister/${pkgname}.sh"
    runhaskell Setup copy --destdir="${pkgdir}"
    rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
